import 'package:flutter/material.dart';

class CheckboxDemo extends StatefulWidget {
  CheckboxDemo({Key? key}) : super(key: key);

  @override
  State<CheckboxDemo> createState() => _CheckboxDemoState();
}

class _CheckboxDemoState extends State<CheckboxDemo> {
  var flag = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("checkbox"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(children: <Widget>[
            Checkbox(
              value: flag,
              onChanged: (v) {
                setState(() {
                  this.flag = v!;
                });
              },
              activeColor: Colors.red,
            )
          ]),
          Row(children: <Widget>[
            Text(this.flag ? "选中" : "未选中"),
          ]),
          SizedBox(
            height: 40,
          ),
          CheckboxListTile(
            onChanged: (v) {},
            value: this.flag,
            title: Text("标题"),
            subtitle: Text("这是一个二级标题"),
          ),
          Divider(),
          CheckboxListTile(
            onChanged: (v) {},
            value: this.flag,
            title: Text("标题"),
            subtitle: Text("这是一个二级标题"),
            secondary: Icon(Icons.help),
            selected: this.flag,
          ),
        ],
      ),
    );
  }
}
